# -*- coding: utf-8 -*-
from datetime import datetime

from peewee import (Model,
                    PrimaryKeyField,
                    IntegerField,
                    TextField,
                    ForeignKeyField,
                    DateTimeField,
                    BooleanField,
                    SqliteDatabase)

db = SqliteDatabase("data.db")


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    uid = PrimaryKeyField()
    phone = IntegerField(unique=True)
    wechat = TextField(null=True)


class Book(BaseModel):
    bid = PrimaryKeyField()
    title = TextField()
    author = TextField(null=True)
    seller = ForeignKeyField(User, backref="books")
    isbn = IntegerField(null=True)
    price = IntegerField()
    update_time = DateTimeField(default=datetime.now)
    cover = TextField(null=True)
    images = TextField(default="[]")
    status = BooleanField(default=True)

class Record(BaseModel):
    rid = PrimaryKeyField()
    user = ForeignKeyField(User, backref="records")
    book = ForeignKeyField(Book, backref="record")
    type = IntegerField()
    price = IntegerField(null=True)
    